package org.zjx.action.actions;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.zjx.action.ActionType;
import org.zjx.action.BaseAction;

import java.io.InvalidClassException;

@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Data
public class MoveAction extends BaseAction {
    private int targetX;
    private int targetY;


    public MoveAction(int targetX, int targetY) {
        this.actionType = ActionType.MOVE;
        this.targetX = targetX;
        this.targetY = targetY;
    }

    public MoveAction(JSONObject json){
        this.actionType = ActionType.MOVE;
        this.targetX = json.getIntValue("targetX");
        this.targetY = json.getIntValue("targetY");
        this.timestamp = json.getLongValue("timestamp");
    }

    @Override
    public JSONObject toJson() {
        JSONObject json = super.toJson();
        json.put("targetX", targetX);
        json.put("targetY", targetY);
        return json;
    }

    @SneakyThrows
    @Override
    public void validate(){
        super.validate();
        // 坐标有效性验证
        if (targetX < 0 || targetY < 0) {
            throw new InvalidClassException("无效的移动坐标");
        }
    }


}
